Исходный файл
C:\WINCE600\PUBLIC\COMMON\OAK\DRIVERS\ETHDBG\EDBGFRMT\format.c
Модифицированный файл
C:\WINCE600\PLATFORM\PracticeBSP_ARK1370\SRC\KITL\format.c
// //
// Copyright (c) Microsoft Corporation.  All rights reserved. // Copyright (c) Microsoft Corporation.  All rights reserved.
// //
// //
// Use of this sample source code is subject to the terms of the Microsoft // Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If // license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not // you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license, // authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable, // please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation. // see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES. // THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
// //
/*++ /*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE. PARTICULAR PURPOSE.
   
Module Name:   Module Name:  
   format.c    format.c
Abstract:   Abstract:  
   This file contains simple formatted output string support for the    This file contains simple formatted output string support for the
   boot loader.    boot loader.
Functions: Functions:
   
   
Notes: Notes:
   supported, with no frills: x d s u X, H, and B    supported, with no frills: x d s u X, H, and B
   backslash n is converted to backslash n backslash r.    backslash n is converted to backslash n backslash r.
   
   X is equivalent to 08x; B, 02x; and H, 04x in printf format.    X is equivalent to 08x; B, 02x; and H, 04x in printf format.
   
--*/  --*/ 
#include <windows.h> #include <windows.h>
#include <nkintr.h> #include <nkintr.h>
   
// //
// Functional Prototypes // Functional Prototypes
// //
static void pOutputByte(unsigned char c); static void pOutputByte(unsigned char c);
static void pOutputNumHex(unsigned long n,long depth); static void pOutputNumHex(unsigned long n,long depth);
static void pOutputNumDecimal(unsigned long n); static void pOutputNumDecimal(unsigned long n);
static void OutputString(const unsigned char *s); static void OutputString(const unsigned char *s);
static void OutputStringW(const WCHAR *s); static void OutputStringW(const WCHAR *s);
   
char *szSprintf; char *szSprintf;
   
// //
// Routine starts // Routine starts
// //
/***************************************************************************** /*****************************************************************************
* *
* *
*   @func   void    | EdbgOutputDebugString | Simple formatted debug output string routine *   @func   void    | EdbgOutputDebugString | Simple formatted debug output string routine
* *
*   @rdesc  none *   @rdesc  none
* *
*   @parm   LPCSTR  |   sz,... | *   @parm   LPCSTR  |   sz,... |
*               Format String: *               Format String:
* *
*               @flag Format string | type *               @flag Format string | type
*               @flag u | unsigned *               @flag u | unsigned
*               @flag d | int *               @flag d | int
*               @flag c | char *               @flag c | char
*               @flag s | string *               @flag s | string
*               @flag x | 4-bit hex number *               @flag x | 4-bit hex number
*               @flag B | 8-bit hex number *               @flag B | 8-bit hex number
*               @flag H | 16-bit hex number *               @flag H | 16-bit hex number
*               @flag X | 32-bit hex number *               @flag X | 32-bit hex number
* *
*   @comm *   @comm
*           Same as FormatString, but output to serial port instead of buffer. *           Same as FormatString, but output to serial port instead of buffer.
*/  */ 
void EdbgOutputDebugString (LPCSTR sz, ...) void EdbgOutputDebugString (LPCSTR sz, ...)
{ {
   unsigned char    c;    unsigned char    c;
   va_list         vl;    va_list         vl;
         
   va_start(vl, sz);    va_start(vl, sz);
         
   while (*sz) {    while (*sz) {
       c = *sz++;        c = *sz++;
       switch (c) {        switch (c) {
       case '%':        case '%':
           c = *sz++;            c = *sz++;
           switch (c) {              switch (c) {  
           case 'x':            case 'x':
               pOutputNumHex(va_arg(vl, unsigned long), 0);                pOutputNumHex(va_arg(vl, unsigned long), 0);
               break;                break;
           case 'B':            case 'B':
               pOutputNumHex(va_arg(vl, unsigned long), 2);                pOutputNumHex(va_arg(vl, unsigned long), 2);
               break;                break;
           case 'H':            case 'H':
               pOutputNumHex(va_arg(vl, unsigned long), 4);                pOutputNumHex(va_arg(vl, unsigned long), 4);
               break;                break;
           case 'X':            case 'X':
               pOutputNumHex(va_arg(vl, unsigned long), 8);                pOutputNumHex(va_arg(vl, unsigned long), 8);
               break;                break;
           case 'd':            case 'd':
               {                {
                   long    l;                    long    l;
                                 
                   l = va_arg(vl, long);                    l = va_arg(vl, long);
                   if (l < 0) {                      if (l < 0) {  
                       pOutputByte('-');                        pOutputByte('-');
                       l = - l;                        l = - l;
                   }                    }
                   pOutputNumDecimal((unsigned long)l);                    pOutputNumDecimal((unsigned long)l);
               }                }
               break;                break;
           case 'u':            case 'u':
               pOutputNumDecimal(va_arg(vl, unsigned long));                pOutputNumDecimal(va_arg(vl, unsigned long));
               break;                break;
           case 's':            case 's':
               OutputString(va_arg(vl, char *));                OutputString(va_arg(vl, char *));
               break;                break;
           case '%':            case '%':
               pOutputByte('%');                pOutputByte('%');
               break;                break;
           case 'c':            case 'c':
               c = va_arg(vl, unsigned char);                c = va_arg(vl, unsigned char);
               pOutputByte(c);                pOutputByte(c);
               break;                break;
                                 
           default:            default:
               pOutputByte(' ');                pOutputByte(' ');
               break;                break;
           }            }
           break;            break;
       case '\r':        case '\r':
           if (*sz == '\n')            if (*sz == '\n')
               sz ++;                sz ++;
           c = '\n';            c = '\n';
           // fall through            // fall through
       case '\n':        case '\n':
           pOutputByte('\r');            pOutputByte('\r');
           // fall through            // fall through
       default:        default:
           pOutputByte(c);            pOutputByte(c);
       }        }
   }    }
         
   va_end(vl);    va_end(vl);
} }
   
/***************************************************************************** /*****************************************************************************
* *
* *
*   @func   void    |   FormatString | Simple formatted output string routine *   @func   void    |   FormatString | Simple formatted output string routine
* *
*   @rdesc  Returns length of formatted string *   @rdesc  Returns length of formatted string
* *
*   @parm   unsigned char * |   pBuf | *   @parm   unsigned char * |   pBuf |
*               Pointer to string to return formatted output.  User must ensure *               Pointer to string to return formatted output.  User must ensure
*               that buffer is large enough. *               that buffer is large enough.
* *
*   @parm   const unsigned char * |   sz,... | *   @parm   const unsigned char * |   sz,... |
*               Format String: *               Format String:
* *
*               @flag Format string | type *               @flag Format string | type
*               @flag u | unsigned *               @flag u | unsigned
*               @flag d | int *               @flag d | int
*               @flag c | char *               @flag c | char
*               @flag s | string *               @flag s | string
*               @flag x | 4-bit hex number *               @flag x | 4-bit hex number
*               @flag B | 8-bit hex number *               @flag B | 8-bit hex number
*               @flag H | 16-bit hex number *               @flag H | 16-bit hex number
*               @flag X | 32-bit hex number *               @flag X | 32-bit hex number
* *
*   @comm *   @comm
*           Same as OutputFormatString, but output to buffer instead of serial port. *           Same as OutputFormatString, but output to buffer instead of serial port.
*/  */ 
unsigned int FormatString (unsigned char *pBuf, const unsigned char *sz, ...) unsigned int FormatString (unsigned char *pBuf, const unsigned char *sz, ...)
{ {
   unsigned char    c;    unsigned char    c;
   va_list         vl;    va_list         vl;
         
   va_start(vl, sz);    va_start(vl, sz);
         
   szSprintf = pBuf;    szSprintf = pBuf;
   while (*sz) {    while (*sz) {
       c = *sz++;        c = *sz++;
       switch (c) {        switch (c) {
       case '%':        case '%':
           c = *sz++;            c = *sz++;
           switch (c) {              switch (c) {  
           case 'x':            case 'x':
               pOutputNumHex(va_arg(vl, unsigned long), 0);                pOutputNumHex(va_arg(vl, unsigned long), 0);
               break;                break;
           case 'B':            case 'B':
               pOutputNumHex(va_arg(vl, unsigned long), 2);                pOutputNumHex(va_arg(vl, unsigned long), 2);
               break;                break;
           case 'H':            case 'H':
               pOutputNumHex(va_arg(vl, unsigned long), 4);                pOutputNumHex(va_arg(vl, unsigned long), 4);
               break;                break;
           case 'X':            case 'X':
               pOutputNumHex(va_arg(vl, unsigned long), 8);                pOutputNumHex(va_arg(vl, unsigned long), 8);
               break;                break;
           case 'd':            case 'd':
               {                {
                   long    l;                    long    l;
                                 
                   l = va_arg(vl, long);                    l = va_arg(vl, long);
                   if (l < 0) {                      if (l < 0) {  
                       pOutputByte('-');                        pOutputByte('-');
                       l = - l;                        l = - l;
                   }                    }
                   pOutputNumDecimal((unsigned long)l);                    pOutputNumDecimal((unsigned long)l);
               }                }
               break;                break;
           case 'u':            case 'u':
               pOutputNumDecimal(va_arg(vl, unsigned long));                pOutputNumDecimal(va_arg(vl, unsigned long));
               break;                break;
           case 's':            case 's':
               OutputString(va_arg(vl, char *));                OutputString(va_arg(vl, char *));
               break;                break;
           case '%':            case '%':
               pOutputByte('%');                pOutputByte('%');
               break;                break;
           case 'c':            case 'c':
               c = va_arg(vl, unsigned char);                c = va_arg(vl, unsigned char);
               pOutputByte(c);                pOutputByte(c);
               break;                break;
                                 
           default:            default:
               pOutputByte(' ');                pOutputByte(' ');
               break;                break;
           }            }
           break;            break;
       case '\r':        case '\r':
           if (*sz == '\n')            if (*sz == '\n')
               sz ++;                sz ++;
           c = '\n';            c = '\n';
           // fall through            // fall through
       case '\n':        case '\n':
           pOutputByte('\r');            pOutputByte('\r');
           // fall through            // fall through
       default:        default:
           pOutputByte(c);            pOutputByte(c);
       }        }
   }    }
   pOutputByte(0);    pOutputByte(0);
   c = szSprintf - pBuf;    c = szSprintf - pBuf;
   szSprintf = 0;    szSprintf = 0;
   va_end(vl);    va_end(vl);
   return (c);    return (c);
} }
   
/***************************************************************************** /*****************************************************************************
* *
* *
*   @func   void    |   pOutputByte | Sends a byte out of the monitor port. *   @func   void    |   pOutputByte | Sends a byte out of the monitor port.
* *
*   @rdesc  none *   @rdesc  none
* *
*   @parm   unsigned int |   c | *   @parm   unsigned int |   c |
*               Byte to send. *               Byte to send.
* *
*/  */ 
static void static void
pOutputByte( pOutputByte(
   unsigned char c    unsigned char c
) )
{ {
   if (szSprintf)    if (szSprintf)
       *szSprintf++ = c;        *szSprintf++ = c;
   else    else
       OEMWriteDebugByte(c);        OEMWriteDebugByte(c);
} }
   
   
/***************************************************************************** /*****************************************************************************
* *
* *
*   @func   void    |   pOutputNumHex | Print the hex representation of a number through the monitor port. *   @func   void    |   pOutputNumHex | Print the hex representation of a number through the monitor port.
* *
*   @rdesc  none *   @rdesc  none
* *
*   @parm   unsigned long |   n | *   @parm   unsigned long |   n |
*               The number to print. *               The number to print.
* *
*   @parm   long | depth | *   @parm   long | depth |
*               Minimum number of digits to print. *               Minimum number of digits to print.
* *
*/  */ 
static void pOutputNumHex (unsigned long n, long depth) static void pOutputNumHex (unsigned long n, long depth)
{ {
   if (depth) {    if (depth) {
       depth--;        depth--;
   }    }
         
   if ((n & ~0xf) || depth) {    if ((n & ~0xf) || depth) {
       pOutputNumHex(n >> 4, depth);        pOutputNumHex(n >> 4, depth);
       n &= 0xf;        n &= 0xf;
   }    }
         
   if (n < 10) {    if (n < 10) {
       pOutputByte((unsigned char)(n + '0'));        pOutputByte((unsigned char)(n + '0'));
   } else {      } else {  
   pOutputByte((unsigned char)(n - 10 + 'A'));    pOutputByte((unsigned char)(n - 10 + 'A'));
} }
} }
   
   
/***************************************************************************** /*****************************************************************************
* *
* *
*   @func   void    |   pOutputNumDecimal | Print the decimal representation of a number through the monitor port. *   @func   void    |   pOutputNumDecimal | Print the decimal representation of a number through the monitor port.
* *
*   @rdesc  none *   @rdesc  none
* *
*   @parm   unsigned long |   n | *   @parm   unsigned long |   n |
*               The number to print. *               The number to print.
* *
*/  */ 
static void pOutputNumDecimal (unsigned long n) static void pOutputNumDecimal (unsigned long n)
{ {
   if (n >= 10) {    if (n >= 10) {
       pOutputNumDecimal(n / 10);        pOutputNumDecimal(n / 10);
       n %= 10;        n %= 10;
   }    }
   pOutputByte((unsigned char)(n + '0'));    pOutputByte((unsigned char)(n + '0'));
} }
   
/***************************************************************************** /*****************************************************************************
* *
* *
*   @func   void    |   OutputString | Sends an unformatted string to the monitor port. *   @func   void    |   OutputString | Sends an unformatted string to the monitor port.
* *
*   @rdesc  none *   @rdesc  none
* *
*   @parm   const unsigned char * |   s | *   @parm   const unsigned char * |   s |
*               points to the string to be printed. *               points to the string to be printed.
* *
*   @comm *   @comm
*           backslash n is converted to backslash r backslash n *           backslash n is converted to backslash r backslash n
*/  */ 
static void OutputString (const unsigned char *s) static void OutputString (const unsigned char *s)
{ {
   while (*s) {             while (*s) {         
       if (*s == '\n') {        if (*s == '\n') {
           OEMWriteDebugByte('\r');            OEMWriteDebugByte('\r');
       }        }
       OEMWriteDebugByte(*s++);        OEMWriteDebugByte(*s++);
   }    }
} }
   
static void OutputStringW(const WCHAR *s) static void OutputStringW(const WCHAR *s)
{ {
   while (*s) {    while (*s) {
       if ((char)*s == '\n') {        if ((char)*s == '\n') {
           OEMWriteDebugByte('\r');            OEMWriteDebugByte('\r');
       }        }
       OEMWriteDebugByte((char )*s++);        OEMWriteDebugByte((char )*s++);
   }    }
} }
   
// This routine will take a binary IP address as represent here and return a dotted decimal version of it    
char *inet_ntoa( DWORD dwIP ) {    
   
   static char szDottedD[16];    
   
   FormatString( szDottedD, "%u.%u.%u.%u",    
       (BYTE)dwIP, (BYTE)(dwIP >> 8), (BYTE)(dwIP >> 16), (BYTE)(dwIP >> 24) );    
   
   return szDottedD;    
   
} // inet_ntoa()    
   
// This routine will take a dotted decimal IP address as represent here and return a binary version of it    
DWORD inet_addr( char *pszDottedD ) {    
   
   DWORD dwIP = 0;    
   DWORD cBytes;    
   char *pszLastNum;    
   int atoi (const char *s);    
       
   // Replace the dots with NULL terminators    
   pszLastNum = pszDottedD;    
   for( cBytes = 0; cBytes < 4; cBytes++ ) {    
       while(*pszDottedD != '.' && *pszDottedD != '\0')    
           pszDottedD++;    
       if (pszDottedD == '\0' && cBytes != 3)    
           return 0;    
       *pszDottedD = '\0';    
       dwIP |= (atoi(pszLastNum) & 0xFF) << (8*cBytes);    
       pszLastNum = ++pszDottedD;    
   }    
   
   return dwIP;    
   
} // inet_ntoa()    
   
/*****************************************************************************    
*    
*   Function: NKDbgPrintfW    
*    
*   Description: Formats and writes a wide character string to the serial port    
*    
*   Parameters: sz  -   pointer to wide character string   
*    
*   Returns: Nothing    
*    
*/    
   
void NKDbgPrintfW(    
   const WCHAR *sz, ...)    
{    
   unsigned char    c;    
   va_list         vl;    
       
   va_start(vl, sz);    
       
   while (*sz) {    
       c = (unsigned char)*sz++;    
       switch (c) {    
           case (unsigned char)'%':    
           c = (unsigned char)*sz++;    
           switch (c) {    
               case 'x':    
               pOutputNumHex(va_arg(vl, unsigned long), 0);    
               break;    
               case 'B':    
               pOutputNumHex(va_arg(vl, unsigned long), 2);    
               break;    
               case 'H':    
               pOutputNumHex(va_arg(vl, unsigned long), 4);    
               break;    
               case 'X':    
               pOutputNumHex(va_arg(vl, unsigned long), 8);    
               break;    
               case 'd': {    
               long    l;    
                   
               l = va_arg(vl, long);    
               if (l < 0) {    
                   pOutputByte('-');    
                   l = - l;    
               }    
               pOutputNumDecimal((unsigned long)l);    
           }    
               break;    
               case 'u':    
               pOutputNumDecimal(va_arg(vl, unsigned long));    
               break;    
               case 's':    
               OutputStringW(va_arg(vl, WCHAR *));    
               break;    
               case '%':    
               pOutputByte('%');    
               break;    
               case 'c':    
               c = va_arg(vl, unsigned char);    
               pOutputByte(c);    
               break;    
                   
               default:    
               pOutputByte(' ');    
               break;    
           }    
           break;    
           case '\n':    
           pOutputByte('\r');    
           // fall through    
           default:    
           pOutputByte(c);    
       }    
   }    
       
   va_end(vl);    
}    
   
void OutputDebugStringW(LPCWSTR fmt)    
{    
   NKDbgPrintfW(fmt);    
}